/*
 * FormGenerate.java
 *
 * Created on August 28, 2007, 10:12 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package util;

import bd.Settings;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 *
 * @author user
 */
public class FormGenerate {
    
    /**
     * Creates a new instance of FormGenerate
     */
    public FormGenerate() 
    {
        
    }
    
     public String generateJavaForm(String namePackage, String nameForm, String fields[], String labels[], 
                                String lengthFields[], String types[], Settings settings)
    {
        String aux= "", name;
        Date dateTime = new Date();
        String form ="";
        int length;
        int widthLabel, widthText, widthTotal=0;
        int col= 20, row=20; 
        
        form = "/*\n"
            +"* frm"+nameForm+".java\n"
            +"*\n"
            +"* Created on "+dateTime.toString()+"\n"
            +"*\n"
            +"*/\n"
            +"\npackage "+namePackage+";\n\n"
            +"import java.awt.*;\n"
            +"import javax.swing.*;\n"
            +"import java.awt.*;\n" 
            +"import java.awt.event.*;\n"
            +"import java.sql.*;\n"                
                
            /*Library temporary*/
            +"import common.*;\n"
                
            +"/**\n"
            +"*\n"
            +"* @author EvolutionSystems\n"
            +"*/\n"
            +"public class frm"+nameForm+" extends JFrame {\n\n"
                +"\tpublic frm"+nameForm+"() {\n"
                    +"\t\tinitComponents();\n"
                +"\t}\n\n"
   
                +"\tprivate void initComponents()\n"
                +"\t{\n"
                    +"\t\tdatabase = new Database();\n"                
                    +"\t\tdatabase.settings.setDatabaseName(\""+settings.getDatabaseName()+"\");\n"
                    +"\t\tdatabase.settings.setDatabaseHost(\""+settings.getDatabaseHost()+"\"); \n"
                    +"\t\tdatabase.settings.setDatabaseUser(\""+settings.getDatabaseUser()+"\"); \n"
                    +"\t\tdatabase.settings.setDatabasePassword(\""+settings.getDatabasePassword()+"\"); \n "
                    +"\t\tdatabase.settings.setDatabaseProtocol(database.POSTGRESQL);\n" /*Wrong*/
                    +"\t\tdatabase.connect();\n"
                    +"\t\tobj"+nameForm+"= new "+nameForm+"(database); \n"                
                    +"\t\tContainer cn = getContentPane();\n"
                    +"\t\tJPanel pnlAux;\n"
                    +"\t\tpnlButtons = new JPanel();\n"
                    +"\t\tpnlButtons.setLayout(new GridLayout(1, nButtons));\n"
                    +"\t\tthis.setTitle(\"Cadastro de "+nameForm+"s\");\n\n"                
                    +"\t\tthis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n\n"
                
                    +"\t\t/*Make the buttons*/\n"
                    +"\t\tbtnButtons = new JButton[nButtons];\n"
                    +"\t\tfor(int i=0; i < nButtons; i++)\n"
                    +"\t\t{\n"
                        +"\t\t\tif( i >= nButtons - 4)\n"
                        +"\t\t\t{\n"
                        +"\t\t\t\tIcon ic = new ImageIcon(\""+namePackage+"/images/\"+ String.valueOf( i - (nButtons - 5) )+\".gif\");\n"
                        +"\t\t\t\tbtnButtons[i] = new JButton(ic);\n"
                        +"\t\t\t}\n"
                        +"\t\t\telse\n"
                        +"\t\t\t\tbtnButtons[i]= new JButton(caption[i]);\n"
                        +"\t\t\tpnlButtons.add(btnButtons[i]);\n"
                    +"\t\t}\n\n"

                    /*Make the button's events*/
                
                    /*Button New*/
                    +"\t\t/*Button New*/\n"
                    +"\t\tbtnButtons[0].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"
                                /*Event button new clicked*/
                                /*TODD: method clear the form***************************************************/
                                +"\t\t\t\t\t tableControl.setSelectedIndex(1);\n"
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"

                    /*Button Save*/
                    +"\t\t/*Button Save*/\n"
                    +"\t\tbtnButtons[1].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"
                                /*Event button Save clicked*/
                                +"\t\t\t\t\t fillObject();\n"
                                +"\t\t\t\t\t obj"+nameForm+".Save();\n"
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"

                    /*Button Edit*/
                    +"\t\t/*Button Edit*/\n"
                    +"\t\tbtnButtons[2].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"
                                /*Event button Update clicked*/
                                +"\t\t\t\t\t showObject();\n"
                                +"\t\t\t\t\t tableControl.setSelectedIndex(1);\n"                                        
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"
                
                    /*Button Cancel*/
                    +"\t\t/*Button Cancel*/\n"
                    +"\t\tbtnButtons[3].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"
                                /*Event button cancel clicked*/
                                /*Think about how to do*************************************/
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"
                
                    /*Button Delete*/
                    +"\t\t/*Button Delete*/\n"
                    +"\t\tbtnButtons[4].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"
                                /*Event button delete clicked*/
                                /*Think about how to do*************************************/                
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"
                
                    /*Button Prior*/
                    +"\t\t/*Button Prior*/\n"                                        
                    +"\t\tbtnButtons[5].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"
                                /*Event button Prior clicked*/                                        
                                +"\t\t\t\t\tindex = gdvSearch.table.getSelectedRow();\n"
                                +"\t\t\t\t\tif( index > 0 )\n"
                                +"\t\t\t\t\t\tgdvSearch.table.setRowSelectionInterval(index - 1, index - 1);\n"
                                +"\t\t\t\t\t\t/*If the tabMain is visible then show the data of object*/\n"        
                                +"\t\t\t\t\t\tif(tableControl.getSelectedIndex() == 1)\n"
                                    +"\t\t\t\t\t\t\tshowObject();\n"
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"
                                        
                    /*Button First*/
                    +"\t\t/*Button First*/\n"
                    +"\t\tbtnButtons[6].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"                                        
                                +"\t\t\t\t\tindex = gdvSearch.table.getRowCount();\n"
                                +"\t\t\t\t\tif( index != - 1)\n"
                                +"\t\t\t\t\t\tgdvSearch.table.setRowSelectionInterval(0, 0);\n\n" 
                                +"\t\t\t\t\t\t/*If the tabMain is visible then show the data of object*/\n"        
                                +"\t\t\t\t\t\tif(tableControl.getSelectedIndex() == 1)\n"
                                    +"\t\t\t\t\t\t\tshowObject();\n"                                        
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"                                        
                
                    /*Button Last*/
                    +"\t\t/*Button Last*/\n"
                    +"\t\tbtnButtons[7].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"                                        
                                +"\t\t\t\t\tindex = gdvSearch.table.getRowCount();\n"
                                +"\t\t\t\t\tif( index != - 1)\n"
                                +"\t\t\t\t\t\tgdvSearch.table.setRowSelectionInterval(index-1, index-1);\n\n"  
                                +"\t\t\t\t\t\t/*If the tabMain is visible then show the data of object*/\n"        
                                +"\t\t\t\t\t\tif(tableControl.getSelectedIndex() == 1)\n"
                                    +"\t\t\t\t\t\t\tshowObject();\n"                                        
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"
                                        
                    /*Button Next*/
                    +"\t\t/*Button Next*/\n"
                    +"\t\tbtnButtons[8].addActionListener( \n"
                        +"\t\t\tnew ActionListener() \n"
                        +"\t\t\t{\n"
                            +"\t\t\t\tpublic void actionPerformed(ActionEvent e) \n"
                            +"\t\t\t\t{ \n"                                        
                                +"\t\t\t\t\tindex = gdvSearch.table.getSelectedRow();\n"
                                +"\t\t\t\t\tif( index >= 0 && index < gdvSearch.table.getRowCount()-1)\n"
                                +"\t\t\t\t\t\tgdvSearch.table.setRowSelectionInterval(index + 1, index + 1);\n\n"
                                +"\t\t\t\t\t\t/*If the tabMain is visible then show the data of object*/\n"        
                                +"\t\t\t\t\t\tif(tableControl.getSelectedIndex() == 1)\n"
                                    +"\t\t\t\t\t\t\tshowObject();\n"                                        
                            +"\t\t\t\t} \n"
                        +"\t\t\t}\n"
                    +"\t\t);\n\n"
                                        
                    +"\t\t/*Make the tabs of Search and 'Cadastro'*/\n"
                    +"\t\ttableControl = new JTabbedPane();\n"
                    +"\t\ttabSearch = new JPanel();\n"
                    +"\t\tsubTabSearch = new JPanel();\n"
                    +"\t\ttabMain = new JPanel();\n"
                    +"\t\ttabMain.setLayout(null);\n\n"
                
                    /*Insert the GridView in the subTabSearch*/
                    +"\t\ttry\n" 
                    +"\t\t{\n"                 
                        +"\t\t\tgdvSearch.fill(obj"+nameForm+".Search());\n"                
                    +"\t\t}\n"                 
                    +"\t\tcatch(SQLException e)\n"                 
                    +"\t\t{\n"                 
                    +"\t\t\tJOptionPane.showMessageDialog(null,e);\n" 
                    +"\t\t\treturn;\n"                 
                    +"\t\t}\n"                 
                    +"\t\tJScrollPane jp = new JScrollPane(gdvSearch.table);\n"
                    +"\t\tsubTabSearch.add(jp);\n"
                    +"\t\ttabSearch.add(subTabSearch, BorderLayout.SOUTH);\n\n";

                    /*Generate the Edits here*/
                    for( int i = 0; i < fields.length; i++)
                    {      
                        widthLabel = labels[i].length()*10;                        
                        widthText  = Integer.parseInt(lengthFields[i]) * 10;
 
                        if( widthLabel < 150)
                            widthLabel = 150;                         
                        if( widthText < 200)
                            widthText = 200;
                        if( widthText > 400)
                            widthText = 400;
                        
                        widthTotal = widthLabel + widthText + col;
                        if( widthTotal > 750)
                        {                           
                            col = 20;
                            row+= 30;
                        }
                                                
                        /*If the component is not a checkbox then it have a label*/
                        if(!types[i].equals("boolean"))
                        {                           
                            aux+= "\t\tlbl"+fields[i]+"= new JLabel(\""+labels[i]+"\");\n";
                            aux+= "\t\tlbl"+fields[i]+".setBounds("+col+","+row+","+widthLabel+",20);\n";                            
                            aux+= "\t\ttabMain.add(lbl"+fields[i]+");\n";
                        }

                        col += widthLabel + 5; 
                                               
                        if( types[i].equals ("int"))
                        {
                            aux+= "\t\ttxt"+fields[i]+"= new JTextField();\n";
                            aux+= "\t\ttxt"+fields[i]+".setBounds("+col+","+row+","+widthText+",20);\n";                             
                            aux+= "\t\ttabMain.add(txt"+fields[i]+");\n\n";                               
                        }
                        else if (types[i].equals ("double"))
                        {
                            aux+= "\t\ttxt"+fields[i]+"= new JTextField();\n";
                            aux+= "\t\ttxt"+fields[i]+".setBounds("+col+","+row+","+widthText+",20);\n";                             
                            aux+= "\t\ttabMain.add(txt"+fields[i]+");\n\n";                               
                        }
                        else if(types[i].equals("boolean"))
                        {
                            aux+= "\t\tchb"+fields[i]+"= new JCheckBox(\""+labels[i]+"\");\n";
                            aux+= "\t\tchb"+fields[i]+".setBounds("+col+","+row+","+widthText+",20);\n";                             
                            aux+= "\t\ttabMain.add(chb"+fields[i]+");\n\n";                               
                        }
                        else
                        {
                            aux+= "\t\ttxt"+fields[i]+"= new JTextField();\n";
                            aux+= "\t\ttxt"+fields[i]+".setBounds("+col+","+row+","+widthText+",20);\n";                             
                            aux+= "\t\ttabMain.add(txt"+fields[i]+");\n\n";                            
                        }
                        col += widthText + 20;                        
                    }
                    form+= aux
                    
                    +"\t\tJScrollPane scrollMain = new JScrollPane(tabMain);\n"
                    +"\t\ttableControl.addTab(\"Pesquisa\",tabSearch);\n"
                    +"\t\ttableControl.addTab(\"Cadastro\",scrollMain);\n\n"

                    +"\t\tcn.add(tableControl,BorderLayout.CENTER);\n"
                    +"\t\tcn.add(pnlButtons,BorderLayout.SOUTH);\n\n"

                    +"\t\tsetSize(800,600);\n"
                +"\t}\n\n"

                +"\tpublic static void main(String[] args) throws SQLException\n"
                +"\t{\n"
                    +"\tnew frm"+nameForm+"().setVisible(true);\n"
                +"\t}\n\n"
                            
                /*Methods*/
                +"\t/*Methods*/\n"
                +"\tprivate void fillObject(){\n";
                    aux = "";
                    for(int i=0; i < fields.length; i++){
                       if( types[i].equals ("int"))
                         aux+="\t\tobj"+nameForm+".set"+fields[i]+"(Integer.parseInt(txt"+fields[i]+".getText()));\n";
                       else if (types[i].equals ("double"))
                         aux+="\t\tobj"+nameForm+".set"+fields[i]+"(Double.parseDouble(txt"+fields[i]+".getText()));\n";
                       else if(types[i].equals("boolean"))
                          aux+="\t\tobj"+nameForm+".set"+fields[i]+"(chb"+fields[i]+".isSelected());\n";
                       else
                          aux+="\t\tobj"+nameForm+".set"+fields[i]+"(txt"+fields[i]+".getText());\n";
                    }

                    form+= aux
                +"\t}\n\n"
                        
                +"\tprivate void fillComponents(){\n";
                    aux = "";
                    for(int i=0; i < fields.length; i++){
                       if( types[i].equals ("int")) //Think in to do
                            aux+= "\t\ttxt"+fields[i]+".setText(String.valueOf(obj"+nameForm+".get"+fields[i]+"()));\n";
                       else if (types[i].equals ("double")) //Think in to do
                            aux+= "\t\ttxt"+fields[i]+".setText(String.valueOf(obj"+nameForm+".get"+fields[i]+"()));\n";
                       else if(types[i].equals("boolean"))
                            aux+= "\t\tchb"+fields[i]+".setSelected(obj"+nameForm+".get"+fields[i]+"());\n";
                       else
                            aux+= "\t\ttxt"+fields[i]+".setText(String.valueOf(obj"+nameForm+".get"+fields[i]+"()));\n";                        
                        
                    }

                    form+= aux
                +"\t}\n\n"                    
                
                +"\tprivate void showObject()\n"
                +"\t{\n"
                   +"\t\tif( gdvSearch.table.getSelectedRow() != -1)\n"
                   +"\t\t{\n";
                       /*Set the id, with theorics is the first field*/                    
                       if( types[0].equals ("int"))
                         form+="\t\t\tobj"+nameForm+".set"+fields[0]+"(Integer.parseInt(gdvSearch.table.getValueAt(gdvSearch.table.getSelectedRow(),0).toString()));\n";
                       else
                          form+="\t\t\tobj"+nameForm+".set"+fields[0]+"(gdvSearch.table.getValueAt(gdvSearch.table.getSelectedRow(),0).toString());\n";

                        form+="\t\t\tobj"+nameForm+".Get();\n" 
                        +"\t\t\tfillComponents();\n"  
                   +"\t\t}\n"
                +"\t}\n\n"
                                                      
                +"\tpublic Database database; \n"                        
                +"\tpublic JPanel pnlButtons;\n"
                +"\tpublic JButton btnButtons[];\n"
                +"\tprivate int nButtons = 9;\n"
                +"\tprivate String[] caption = {\"Novo\",\"Gravar\",\"Alterar\",\"Cancelar\",\"Excluir\"};\n"
                +"\tprivate JTabbedPane tableControl;\n"
                +"\tprivate JPanel tabSearch;\n"
                +"\tprivate JPanel tabMain;\n"
                +"\tprivate JPanel subTabSearch;\n"
                +"\tGridView gdvSearch = new GridView();\n"
                +"\tprivate "+nameForm+" obj"+nameForm+"; \n"
                +"\tprivate int index;";
                        
               /*Declare all components here*/
                aux = "";

                for( int i = 0; i < fields.length; i++)
                {
                    name = fields[i];
                    aux+= "\tprivate JLabel lbl"+name+";\n";                    
                   if( types[i].equals ("int")) //Think in to do
                        aux+= "\tprivate JTextField txt"+name+";\n";
                   else if (types[i].equals ("double")) //Think in to do
                        aux+= "\tprivate JTextField txt"+name+";\n";
                   else if(types[i].equals("boolean"))
                        aux+= "\tprivate JCheckBox chb"+name+";\n";
                   else
                        aux+= "\tprivate JTextField txt"+name+";\n";  
                }
                form+= aux
            +"}\n";
        
        return form;
    }
    
   
     public String generateDelphiDfmForm(String nameForm, String fields[], String labels[], 
                                String lengthFields[], String types[])
   {
      String form ="", aux = "";  
      int row = 0;
      
      /*form =  

      "object frm"+nameForm+": Tfrm"+nameForm+"\n"
        +"\tLeft = 0\n"
        +"\tTop = 0\n"
        +"\tCaption = '"+nameForm+"'\n"
        +"\tClientHeight = "+(fields.length*45)+"\n"
        +"\tClientWidth = 383\n"
        +"\tColor = clBtnFace\n"
        +"\tFont.Charset = DEFAULT_CHARSET\n"
        +"\tFont.Color = clWindowText\n"
        +"\tFont.Height = -11\n"
        +"\tFont.Name = 'Tahoma'\n"
        +"\tFont.Style = []\n"
        +"\tOldCreateOrder = False\n"
        +"\tPixelsPerInch = 96\n"
        +"\tTextHeight = 13\n";
     */
      //TODO: pegar do lugar indicado pelo usuario
         try 
         {
            BufferedReader in = new BufferedReader(new FileReader("res/templates/delphi/TemplUFCadastro/UFForm.dfm"));
            while (in.ready()) 
               form += in.readLine()+"\n";
            in.close();
         } 
         catch (IOException e) 
         {
         }
      
         for( int i = 0; i < fields.length; i++)
         {      
            /*If the component is checkbox then it not have a label*/
            if(!types[i].equals("boolean"))
            {                           
               aux+= "\tobject lbl"+labels[i]+": TLabel\n";
               aux+="\t\tLeft = 24\n";
               aux+="\t\tTop = "+((row++)*21)+"\n";
               aux+="\t\tWidth = "+(labels[i].length()*6)+"\n";
               aux+="\t\tHeight = 13\n";
               aux+="\t\tCaption = '"+labels[i]+"'\n";            
               aux+="\tend\n";
            }

            if( types[i].equals ("int"))
            {
               aux+= "\tobject txt"+labels[i]+": TEdit\n"; 
               aux+="\t\tLeft = 24\n";
               aux+="\t\tTop = "+((row++)*21)+"\n";
               aux+="\t\tWidth = 250\n";
               aux+="\t\tHeight = 13\n";
               aux+="\t\tTabOrder = "+i+"\n";
               aux+="\t\tText = ''\n";                         
               aux+="\tend\n";            
            }
            else if (types[i].equals ("double"))
            {
               aux+= "\tobject txt"+labels[i]+": TEdit\n";
               aux+="\t\tLeft = 24\n";
               aux+="\t\tTop = "+((row++)*21)+"\n";
               aux+="\t\tWidth = 250\n";
               aux+="\t\tHeight = 13\n";
               aux+="\t\tTabOrder = "+i+"\n";
               aux+="\t\tText = ''\n";             
               aux+="\tend\n";               
            }
            else if(types[i].equals("boolean"))
            {
               aux+= "\tobject chb"+labels[i]+": TCheckBox\n";
               aux+="\t\tLeft = 24\n";
               aux+="\t\tTop = "+((row++)*21)+"\n";
               aux+="\t\tWidth = "+(labels[i].length()*12)+"\n";
               aux+="\t\tHeight = 13\n";
               aux+="\t\tTabOrder = "+i+"\n";
               aux+="\t\tCaption = '"+labels[i]+"'\n";                
               aux+="\tend\n";            
            }
            else
            {  
               aux+= "\tobject txt"+labels[i]+": TEdit\n";
               aux+="\t\tLeft = 24\n";
               aux+="\t\tTop = "+((row++)*21)+"\n";
               aux+="\t\tWidth = 250\n";
               aux+="\t\tHeight = 13\n";
               aux+="\t\tTabOrder = "+i+"\n";
               aux+="\t\tText = ''\n";              
               aux+="\tend\n";               
            }                       
         }  
        
         /*form+=aux 
         +"end";*/
      
        form= form.replace("{NAME}", nameForm);
        
        form= form.replace("{OBJECTS}", aux);
         
         return form;
     }
    
        
   public String generateDelphiPasForm(String nameForm, String fields[], String labels[], 
                          String lengthFields[], String types[])
   {  
      Date dateTime = new Date();
      String form ="", aux=""; 
        
      /*form = "{*\n"
      +"* frm"+nameForm+"\n"
      +"*\n"
      +"* Created on "+dateTime.toString()+"\n"
      +"*\n"
      +"*}\n\n"        

      +"unit u_"+nameForm+";\n\n"

      +"interface\n\n"

      +"uses\n"
      +"\tWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,\n"
      +"\tDialogs, StdCtrls;\n\n"

      +"type\n"
      +"\tTfrm"+nameForm+" = class(TForm)\n";*/

      try 
      {
         BufferedReader in = new BufferedReader(new FileReader("res/templates/delphi/TemplUFCadastro/UFForm.pas"));
         while (in.ready()) 
            form += in.readLine()+"\n";
         in.close();
      } 
      catch (IOException e) 
      {
      }
      
      /*Generate the Edits here*/
      for( int i = 0; i < fields.length; i++)
      {      
         /*If the component is checkbox then it not have a label*/
         if(!types[i].equals("boolean"))
         {                           
            aux+= "\t\tlbl"+labels[i]+": TLabel;\n";
         }

         if( types[i].equals ("int"))
         {
            aux+= "\t\ttxt"+labels[i]+": TEdit;\n";                                                       
         }
         else if (types[i].equals ("double"))
         {
            aux+= "\t\ttxt"+labels[i]+": TEdit;\n";                             
         }
         else if(types[i].equals("boolean"))
         {
            aux+= "\t\tchb"+labels[i]+": TCheckBox;\n";                            
         }
         else
         {
            aux+= "\t\ttxt"+labels[i]+": TEdit;\n";                           
         }                       
      }            

      /*form+= aux 
      +"\n\tprivate\n"
      +"\t\t{ Private declarations }\n"               
      +"\tpublic\n"
      +"\t\t{ Public declarations }\n"
      +"\tend;\n\n"

      +"var\n"
      +"\tfrm"+nameForm+": Tfrm"+nameForm+";\n\n"

      +"implementation\n\n"

      +"{$R *.dfm}\n\n"

      +"end.";*/

        form= form.replace("{NAME}", nameForm);
        
        form= form.replace("{OBJECTS}", aux);
        
      return form;
   }    


   public String generateCSharpForm(String namespace, String nameForm, String fields[], String labels[], 
                                String lengthFields[], String types[])
   {
      String form, aux;
      String objectName = nameForm.toLowerCase();
      int i;
      Double length;
      
      form = "";
      try 
      {
         BufferedReader in = new BufferedReader(new FileReader("res/templates/C#/RegisterTemplate/frmRegister.cs"));
         while (in.ready()) 
           form += in.readLine()+"\n";
         in.close();
      } 
      catch (IOException e) 
      {
      }       
      
      //Namespace
      form = form.replace("{NAMESPACE}", namespace);
      
      //Name of Form
      form = form.replace("{NAME}", nameForm);
      
      form = form.replace("{OBJECTNAME}", objectName);
      
      //Delete Method
      aux = "return "+objectName+".delete();";
      form = form.replace("{DELETE}", aux);
      
      //FillDataGridView
		aux = "int tam;\n\n";
			
		aux +="\t\t\tlist"+nameForm+".fillList();\n";
		aux +="\t\t\tdgvMain.Rows.Clear();\n";
		aux +="\t\t\ttam = list"+nameForm+".count();\n";
		aux +="\t\t\tfor (int i=0; i < tam; i++)\n";
		aux +="\t\t\t{\n";
		aux +="\t\t\t\tdgvMain.Rows.Add(new String[] {";
      for(i=0; i < fields.length; i++)
      {
         aux += "\n\t\t\t\t\t\tlist"+nameForm+".get(i)."+fields[i].replace(objectName+"_","")+", ";
		}      
      //Retira o ultimo espaco e a virgula
      aux = aux.substring(0, aux.length() - 2);
      aux += "});\n";
      aux += "\t\t\t}";
      
      form = form.replace("{FILLDATAGRIDVIEW}", aux);
      
      //Init
      aux = "";
      for(i=0; i < fields.length; i++)
      {
         aux+= "\t\t\tDataGridViewTextBoxColumn "+fields[i]+"= new DataGridViewTextBoxColumn();\n";
      }
      
      for(i=0; i < fields.length; i++)
      {
         length = Double.parseDouble(lengthFields[i]);
         if (length > 100)
            length = 100.0;
         aux+= "\n\t\t\t"+fields[i]+".Name=\t\t\""+fields[i]+"\";\n";
         aux+= "\t\t\t"+fields[i]+".HeaderText=\t\t\""+labels[i]+"\";\n";
         aux+= "\t\t\t"+fields[i]+".Width=\t\t"+(Math.round(length*4))+";\n";
      }
      
      aux += "\n\t\t\tdgvMain.Columns.AddRange(new DataGridViewColumn[] {";
      for (i=0; i < fields.length; i++)
      {
         aux+= "\n\t\t\t\t\t"+fields[i]+", ";
      }
      //Retira o ultimo espaco e a virgula
      aux = aux.substring(0, aux.length() - 2);
      aux += "});";
      
      form = form.replace("{INIT}", aux);
      
      //TODO: NewEvent
      
      //Save
      aux = "return "+objectName + ".save();";
      form = form.replace("{SAVE}", aux);
      
      //SetClass
      aux = "";
      for (i = 0; i < fields.length; i++)
      {      
         if( types[i].equals (Constants.INT))
         {
            aux += "\t\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+"=\t\ttxt"+fields[i]+".Text;\n";
         }
         else if (types[i].equals (Constants.DOUBLE))
         {
            aux += "\t\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+"=\t\ttxt"+fields[i]+".Text;\n";                          
         }
         else if(types[i].equals(Constants.BOOLEAN))
         {
            aux += "\t\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+"=\t\tcbx"+fields[i]+".Checked;\n";
         }
         else
         {
            aux += "\t\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+"=\t\ttxt"+fields[i]+".Text;\n";
         }                  
      }
      form = form.replace("{SETCLASS}", aux);
      
      //SetEdits
      aux = "";
      for (i = 0; i < fields.length; i++)
      {      
         if( types[i].equals (Constants.INT))
         {
            aux += "\t\t\ttxt"+fields[i]+".Text=\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+";\n";
         }
         else if (types[i].equals (Constants.DOUBLE))
         {
            aux += "\t\t\ttxt"+fields[i]+".Text=\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+";\n";                          
         }
         else if(types[i].equals(Constants.BOOLEAN))
         {
            aux += "\t\t\tcbx"+fields[i]+".Checked=\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+";\n";
         }
         else
         {
            aux += "\t\t\ttxt"+fields[i]+".Text=\t\t"+objectName+"."+fields[i].replace(objectName+"_","")+";\n";
         }                  
      }
      form = form.replace("{SETEDITS}", aux);
      
      //Update
      aux = "return "+objectName+".update();";
      
      form= form.replace("{UPDATE}", aux);
      
      return form;
   }
   
   public String generateCSharpDesignerForm(String namespace, String nameForm, String fields[], String labels[], 
                                String lengthFields[], String types[])
   {
      String form, aux;
      String objectName = nameForm.toLowerCase();
      int i, row;
      Double length;
      
      //TODO: Testar se e para usar kriptontoolkit
      String label = "ComponentFactory.Krypton.Toolkit.KryptonLabel";
      String textBox = "ComponentFactory.Krypton.Toolkit.KryptonTextBox";
      String checkBox = "ComponentFactory.Krypton.Toolkit.KryptonCheckBox";
      
      form = "";
      try 
      {
         BufferedReader in = new BufferedReader(new FileReader("res/templates/C#/RegisterTemplate/frmRegister.Designer.cs"));
         while (in.ready()) 
           form += in.readLine()+"\n";
         in.close();
      } 
      catch (IOException e) 
      {
      }       
      
      //Namespace
      form = form.replace("{NAMESPACE}", namespace);
      
      //Name of Form
      form = form.replace("{NAME}", nameForm);


      //Object inicializaion
      aux = "";
      for (i = 0; i < fields.length; i++)
      {      
         if(types[i].equals(Constants.BOOLEAN))
         {
            aux += "\t\t\tthis.cbx"+fields[i]+"=\t\tnew "+checkBox+"();\n";
         }
         else
         {
            aux += "\t\t\tthis.lbl"+fields[i]+"=\t\tnew "+label+"();\n";
            aux += "\t\t\tthis.txt"+fields[i]+"=\t\tnew "+textBox+"();\n";
         }                  
      }      
      
      //Suspend layout
      for (i = 0; i < fields.length; i++)
      {      
         if(types[i].equals(Constants.BOOLEAN))
         {
            aux += "\t\t\tthis.this.tabPrincipal.Controls.Add(this.cbx"+fields[i]+");\n";
         }
         else
         {
            aux += "\t\t\tthis.tabPrincipal.Controls.Add(this.lbl"+fields[i]+");\n";
            aux += "\t\t\tthis.tabPrincipal.Controls.Add(this.txt"+fields[i]+");\n";
         }                  
      }  
      
      row = 18;
      for (i=0; i < fields.length; i++)
      {
         if(types[i].equals(Constants.BOOLEAN))
         {
			   aux+= "\t\t\t//\n";
			   aux+= "\t\t\t// cbx"+fields[i]+"\n";
			   aux+= "\t\t\t//\n";
			   aux+= "\t\t\tthis.cbx"+fields[i]+".Checked = true;\n";
			   aux+= "\t\t\tthis.cbx"+fields[i]+".CheckState = System.Windows.Forms.CheckState.Checked;\n";
			   aux+= "\t\t\tthis.cbx"+fields[i]+".Location = new System.Drawing.Point(6, "+row+");\n";
			   aux+= "\t\t\tthis.cbx"+fields[i]+".Name = \"cbx"+fields[i]+"\";\n";
            //TODO: arrumar tamanho de acordo com o label
			   aux+= "\t\t\tthis.cbx"+fields[i]+".Size = new System.Drawing.Size(104, 24);\n";
			   aux+= "\t\t\tthis.cbx"+fields[i]+".TabIndex = "+i+";\n";
			   aux+= "\t\t\tthis.cbx"+fields[i]+".Text = \""+labels[i]+"\";\n";
			   aux+= "\t\t\tthis.cbx"+fields[i]+".UseVisualStyleBackColor = true;\n";
            row += 22;
         }         
         else
         {
            length = Double.parseDouble(lengthFields[i]);
            if (length > 100)
               length = 100.0;            
            aux+= "\t\t\t//\n";
            aux+= "\t\t\t// lbl"+fields[i]+"\n";
            aux+= "\t\t\t//\n";
			   aux+= "\t\t\tthis.lbl"+fields[i]+".Location = new System.Drawing.Point(6, "+row+");\n";
			   aux+= "\t\t\tthis.lbl"+fields[i]+".Name = \"lbl"+fields[i]+"\";\n";
			   aux+= "\t\t\tthis.lbl"+fields[i]+".Size = new System.Drawing.Size(41, 19);\n";           
            aux+= "\t\t\tthis.lbl"+fields[i]+".Values.ExtraText = \"\";\n";
            aux+= "\t\t\tthis.lbl"+fields[i]+".Values.Image = null;\n";
            aux+= "\t\t\tthis.lbl"+fields[i]+".Values.Text = \""+labels[i]+"\";\n\n";

            aux+= "\t\t\t//\n";
            aux+= "\t\t\t// txt"+fields[i]+"\n";
            aux+= "\t\t\t//\n";            
			   //aux+= "this.txtCode.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
			   aux+= "\t\t\tthis.txt"+fields[i]+".Location = new System.Drawing.Point(6, "+(row+20)+");\n";
			   aux+= "\t\t\tthis.txt"+fields[i]+".Name = \"txt"+fields[i]+"\";\n";
            //TODO: Melhorar codigo, maxlength
			   aux+= "\t\t\tthis.txt"+fields[i]+".Size = new System.Drawing.Size("+Math.round(length * 4)+", 20);\n";
			   aux+= "\t\t\tthis.txt"+fields[i]+".TabIndex = "+i+";\n";
			   //aux+= "this.txt"+fields[i]+".Tag = "Código";
            row += 42;
         }         
      }
      
      form = form.replace("{OBJECTS}", aux);

      //Private objects
      aux = "";
      for (i=0; i < fields.length; i++)
      {
         if(types[i].equals(Constants.BOOLEAN))
         {
            aux+= "\t\tprivate "+checkBox+" chk"+fields[i]+";\n";
         }         
         else
         {
            aux+= "\t\tprivate "+label+" lbl"+fields[i]+";\n";
            aux+= "\t\tprivate "+textBox+" txt"+fields[i]+";\n";
         }         
      } 
      
      form = form.replace("{PRIVATEOBJECTS}", aux);
      
      return form;
   }
}
